<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\file\FileInput;

$pass_data = array_merge(Yii::$app->params['area']['province_data'], Yii::$app->params['area']['country_data']);
?>
<style type="text/css">
    .customer-form { font-size:12px;padding-bottom:30px; }    
    .customer-form .form-group { margin-bottom:5px;}
    .customer-form .form-control {height:28px;}
    .none {display:none;}
    .show {display:block; padding:10px 0 0 10px;}
    .ui-widget.ui-widget-content {z-index:9999; /* 自动补全　在模态框下不显示 */ } 
    .warning{float: right;margin-top: -36px;margin-right: -20px;color: #F09303}
</style>

<?=  yii\web\View::registerCssFile("/js/viewer.css",['depends' => 'app\assets\AppAsset']) ?>
<?=  yii\web\View::registerCssFile("/js/jquery-ui/jquery-ui.min.css",['depends' => 'app\assets\AppAsset']) ?>
<?=  yii\web\View::registerJsFile("/js/jquery-ui/jquery-ui.min.js",['depends' => 'app\assets\AppAsset']) ?>
<?=  yii\web\View::registerJsFile("/js/mask/jquery.mask.min.js",['depends' => 'app\assets\AppAsset']) ?>
<?=  yii\web\View::registerJsFile("/js/viewer.js",['depends' => 'app\assets\AppAsset']) ?>

<div class="customer-form row">
    <?php $form = ActiveForm::begin([
                                 'options' => ['enctype' => 'multipart/form-data',],
                                 'layout' => 'horizontal',
                                 'fieldConfig' => [
                                    'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                                    'horizontalCssClasses' => [
                                        'label' => 'col-sm-4',
                                        'wrapper' => 'col-sm-8',
                                        'error' => '',
                                    ],
                                 ],
                            ]); ?>


    <div class="col-md-5">

    <?= $form->field($model, 'room_use_id')->label(false)->hiddenInput() ?>
    <?= $form->field($model, 'room_id')->label(false)->hiddenInput() ?>
    
    <div class="form-group field-customer-first_name">
        <label class="control-label col-sm-4" for="customer-first_name">中文姓</label>
        <div class="col-sm-4">
            <input type="text" id="customer-first_name" class="form-control" name="Customer[first_name]" value="<?= $model->first_name ?>" maxlength="32">

            <div class="help-block help-block-error "></div>
        </div>
        <div class="col-sm-4">
            <input type="text" id="customer-en_first_name" class="form-control" name="Customer[en_first_name]" value="<?= $model->en_first_name ?>" >
            <div class="help-block help-block-error "></div>
        </div>
    </div>


    <div class="form-group field-customer-first_name">
        <label class="control-label col-sm-4" for="customer-last_name">中文名</label>
        <div class="col-sm-4">
            <input type="text" id="customer-last_name" class="form-control" name="Customer[last_name]" value="<?= $model->last_name ?>" maxlength="32">
            <div class="help-block help-block-error"></div>
        </div>
        <div class="col-sm-4">
            <input type="text" id="customer-en_last_name" class="form-control" name="Customer[en_last_name]" value="<?= $model->en_last_name ?>" >
            <div class="help-block help-block-error "></div>
        </div>
    </div>

   
    <div class="form-group field-customer-passport" style="margin-bottom:15px;">
        <label class="control-label col-sm-4" for="customer-passport">护照号码</label>
        <div class="col-sm-8">
            <input type="text" id="customer-passport" class="form-control" name="Customer[passport]" maxlength="32" value="<?= $model->passport ?>" >
            <div class="block-error "></div>
        </div>
    </div>


    <div class="form-group field-customer-native">
        <label class="control-label col-sm-4" for="customer-native">出生地</label>
        <div class="col-sm-8">
            <input name="Customer[native]" id="customer-native" class="form-control" type="text"   value="<?= $model->native ?>" />
            <div class="help-block help-block-error "></div>
        </div>
    </div>

    


    <div class="form-group field-customer-passport_add">
        <label class="control-label col-sm-4" for="customer-passport_add">护照签发地</label>
        <div class="col-sm-8">
            <input name="Customer[passport_add]" id="customer-passport_add" class="form-control" type="text"   value="<?= $model->passport_add ?>" />
            <div class="help-block help-block-error "></div>
        </div>
    </div>

    <div class="form-group field-customer-passport_grant">
        <label class="control-label col-sm-4" for="customer-passport_grant">护照签发日期</label>
        <div class="col-sm-8">

            <input name="Customer[passport_grant]" id="customer-passport_grant" placeholder="YYYYMMDD" class="simple-field-data-mask form-control" type="text" data-mask="0000-00-00" value="<?= $model->passport_grant ?>" />
        <div class="help-block help-block-error "></div>
        </div>
    </div>

    <div class="form-group field-customer-passport_date">
        <label class="control-label col-sm-4" for="customer-passport_date">护照有效期</label>
        <div class="col-sm-8">
            <input name="Customer[passport_date]" id="customer-passport_date" placeholder="YYYYMMDD" class="simple-field-data-mask form-control" type="text" data-mask="0000-00-00"  value="<?= $model->passport_date ?>"/>
        <div class="help-block help-block-error "></div>
        </div>
    </div>

    <div class="form-group field-customer-country">
        <label class="control-label col-sm-4" for="customer-country">国籍</label>
        <div class="col-sm-8">
            <input name="Customer[country]" id="customer-country" placeholder="国籍" class="form-control" type="text"   value="<?= $model->country ?>" />
            <div class="help-block help-block-error "></div>
        </div>
    </div>

    <?= $form->field($model, 'card')->textInput(['maxlength' => 18]) ?>

    <?= $form->field($model, 'gender')->inline(true)->radioList(\Yii::$app->params['gender']) ?>
   
    <div class="form-group field-customer-birthday">
            <label class="control-label col-sm-4" for="customer-birthday">出生日期</label>
            <div class="col-sm-8">
                <input name="Customer[birthday]" id="customer-birthday" placeholder="YYYYMMDD" class="simple-field-data-mask form-control" type="text" data-mask="0000-00-00"  value="<?= $model->birthday ?>"/>
            <div class="help-block help-block-error "></div>
            </div>
    </div>

    <?= $form->field($model, 'group')->textInput(['maxlength' => true]) ?>
    
    </div>

    <div class="col-md-7">
        <div style="margin-top: 15px;">
       
        
        <?= $form->field($model, 'supply')->textInput(['maxlength' => true]) ?>
        <?= $form->field($model, 'size')->textInput(['maxlength' => true]) ?> 
        <?= $form->field($model, 'color')->textInput(['maxlength' => true]) ?>
        <?= $form->field($model, 'ticket')->textInput(['maxlength' => true]) ?>
        <?= $form->field($model, 'phone')->textInput(['maxlength' => true]) ?>
        <?= $form->field($model, 'remark')->textArea(['maxlength' => true]) ?>
        <?= $form->field($model, 'status')->inline(true)->radioList(\Yii::$app->params['status']) ?>

        </div>
    </div>

    <div class="col-md-12" style="margin-top: 10px;border:1px solid #ddd;padding:10px 10px;">
        <?= $form->field($model, 'cancel')->inline(true)->radioList(\Yii::$app->params['cancel']) ?>
    </div>    

    <hr style="margin-top:10px;margin-bottom:10px;">

    <div class="col-md-12 text-center" style="margin-top: 20px">
   
        <?= Html::submitButton($model->isNewRecord ? '添加客户' : '确认修改', ['class' =>  'col-md-2  col-md-offset-5 btn btn-success ']) ?>
  
    </div>

    <?php ActiveForm::end(); ?>

</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">

    $("#customer-first_name").change(function(){//{{{
        var name = $(this).val(); // 获取客户编号

        $.get("/cruise/customer/pinyin", {name:name}, function(data){
            if(data){
                $("#customer-en_first_name").val(data);
            }
        });
    });//}}}

    $("#customer-last_name").change(function(){//{{{
        var name = $(this).val();

        $.get("/cruise/customer/pinyin", {name:name}, function(data){
            if(data){
                $("#customer-en_last_name").val(data);
            }
        });
    });//}}}

    // 护照长度判断
    $("#customer-passport").change(function(){//{{{
        var name = $(this).val();
        
        console.log(name.length);
        if(name.length != 9){
            $(this).siblings(".block-error").text("护照长度不是 9 位");    
        }else{
            $(this).siblings(".block-error").text("");  
        }
    });//}}}

function isValidateDate(date) {//{{{
    date = $.trim(date);
    var reg = /^(\d{4})-(\d{2})-(\d{2})$/;
    reg.exec(date);
    if (!reg.test(date) && RegExp.$2 <= 12 && RegExp.$3 <= 31) {
            return false;
    }
    var year, month, day;
    year = parseInt(date.split("-")[0], 10);
    month = parseInt(date.split("-")[1], 10);
    day = parseInt(date.split("-")[2], 10);
    if(1996>year){
        return false;   
    }

    if (! ((1 <= month) && (12 >= month) && (31 >= day) && (1 <= day))) {
        return false;
    }
    if ((month <= 7) && ((month % 2) == 0) && (day >= 31)) {
        return false;
    }
    if ((month >= 8) && ((month % 2) == 1) && (day >= 31)) {
            return false;
    }
    if (month == 2) {
            if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
               if (day > 29) {
                       return false;
               }
          } else {
               if (day > 28) {
                   return false;
               }
          }
    }
    return true;
}//}}}


    // 护照日期自动赋值
    $("#customer-passport_grant").change(function(){//{{{
        var passport = $(this).val(); 
        
        var ret = isValidateDate(passport);
        if(! ret){
            
            alert("抱歉，您输入的日期格式有误, 请仔细检查");
        }

        $.get("/cruise/customer/passport", {passport:passport}, function(data){

            var val = $("#ordercustomer-passport_date").val();
            if(data && (val != data)){
                $("#customer-passport_date").val(data);
            }
        });
       
    });//}}}

    // 国籍是否是中国
    $("#customer-country").bind("input propertychange change",function(event){
        var name = $(this).val();

        if(name == '中国'){
            $('.card').removeClass('none');                                     
            $('.card').addClass('show');                                        
        }else{
            $('.card').addClass('none');                                        
            $('.card').removeClass('show');                                     
        }
    });


    // 身份证信息自动填充赋值
    $("#customer-card").change(function(){
        var card = $(this).val(); // 获取身份证号
        //console.log(card);

        $.get("/cruise/customer/card", {card:card}, function(data){
            if(data){
                if(data.card_address){
                    $("#customer-card_address").val(data.card_address);
                }
                //if(data.native){
                //  $("#customer-native").val(data.native);
                //}
                if(data.birthday){
                    $("#customer-birthday").val(data.birthday);
                }
                if(data.gender){
                    $("input[name='customer[gender]']").prop("checked", false);
                    $("input[name='customer[gender]'][value="+data.gender+"]").prop("checked",true);
                }
            }
        })

    });

    // 护照签发地 自动补全
    var pass_data = [<?php foreach($pass_data as $pd){  echo '"'. $pd .'"' .  ',';} ?>];

    $("#customer-passport_add").autocomplete({
        source:pass_data 
    });

    // 国籍 自动补全
    var country = [<?php foreach(\Yii::$app->params['area']['country_data'] as $pd){  echo '"'. $pd .'"' .  ',';} ?>];

    $("#customer-country").autocomplete({
        source: country
    });

    // 出生地 自动补全
    var _native = [<?php foreach($pass_data as $pd){  echo '"'. $pd .'"' .  ',';} ?>];

    $("#customer-native").autocomplete({
        source: _native
    });


   

</script>
<?php app\components\JsBlock::end() ?>
